package com.yunji.storage.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yunji.common.annotation.EventRecord;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.constant.EventBusinessTypeConstant;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.storage.bo.RepairRemoveBO;
import com.yunji.storage.bo.RepairSelectQueryBO;
import com.yunji.storage.bo.StorageUpdateAuditBO;
import com.yunji.storage.constant.StorageEventTypeConstant;
import com.yunji.storage.dao.IRepairTicketDao;
import com.yunji.storage.domain.RepairTicketDO;
import com.yunji.storage.domain.StockTakeDO;
import com.yunji.storage.enums.RepairStatus;
import com.yunji.storage.mapper.RepairTicketMapper;
import org.springframework.stereotype.Repository;

import java.util.Arrays;

@Repository
public class RepairTicketDaoImpl extends BaseDaoImpl<RepairTicketMapper, RepairTicketDO> implements IRepairTicketDao {
    @Override
    public RepairTicketDO selectVO(RepairSelectQueryBO bo) {
        return getOne(new LambdaQueryWrapper<RepairTicketDO>()
                .eq(BeanUtils.isNotNull(bo.getBelongId()), RepairTicketDO::getBelongId, bo.getBelongId())
                .eq(BeanUtils.isNotNull(bo.getRepairCode()), RepairTicketDO::getRepairCode, bo.getRepairCode())
                .eq(BeanUtils.isNotNull(bo.getRepairStatus()), RepairTicketDO::getRepairStatus, bo.getRepairStatus()));
    }

    @Override
    public boolean deleteByTicket(RepairRemoveBO bo) {
        return remove(new LambdaQueryWrapper<RepairTicketDO>()
                .eq(BeanUtils.isNotNull(bo.getBelongId()), RepairTicketDO::getBelongId, bo.getBelongId())
                .eq(BeanUtils.isNotNull(bo.getRepairStatus()), RepairTicketDO::getRepairStatus, bo.getRepairStatus()));
    }

    @Override
    public boolean deleteDraft(Long... repairIds) {
        return remove(new LambdaQueryWrapper<RepairTicketDO>()
                .in(RepairTicketDO::getRepairId, Arrays.asList(repairIds))
                .eq(RepairTicketDO::getRepairStatus, RepairStatus.DRAFT.getCode()));
    }

//    @Override
//    public Integer countRepair(RepairCountBO bo) {
//        Long count = lambdaQuery().eq(bo.getBelongId() != null, RepairTicket::getBelongId, bo.getBelongId())
//                .eq(bo.getAuditStatus() != null, RepairTicket::getAuditStatus, bo.getAuditStatus()).count();
//        return Optional.fromNullable(count).or(0L).intValue();
//    }

    @Override
    public void resetAuditStatus(Long repairId) {
        this.baseMapper.resetAuditStatus(repairId);
    }

    @Override
    @EventRecord(businessType = EventBusinessTypeConstant.StorageType.EVENT_STORAGE_REPAIR,eventType = StorageEventTypeConstant.EVENT_TYPE_STORAGE)
    public boolean create(RepairTicketDO repairTicket) {
        return save(repairTicket);
    }

    @Override
    @EventRecord(businessType = EventBusinessTypeConstant.StorageType.EVENT_STORAGE_REPAIR,eventType = StorageEventTypeConstant.EVENT_TYPE_STORAGE)
    public boolean updateTicket(RepairTicketDO repairTicket) {
        return updateById(repairTicket);
    }

    @Override
    @EventRecord(businessType = EventBusinessTypeConstant.StorageType.EVENT_STORAGE_REPAIR,eventType = StorageEventTypeConstant.EVENT_TYPE_AUDIT)
    public boolean updateAuditStatus(StorageUpdateAuditBO auditBO) {
        return update(new LambdaUpdateWrapper<RepairTicketDO>()
                .set(BeanUtils.isNotNull(auditBO.getAuditStatus()), RepairTicketDO::getAuditStatus,auditBO.getAuditStatus())
                .set(BeanUtils.isNotNull(auditBO.getAuditId()),RepairTicketDO::getAuditId,auditBO.getAuditId())
                .set(BeanUtils.isNotNull(auditBO.getAuditName()),RepairTicketDO::getAuditName,auditBO.getAuditName())
                .set(BeanUtils.isNotNull(auditBO.getAuditBelongId()),RepairTicketDO::getAuditBelongId,auditBO.getAuditBelongId())
                .set(BeanUtils.isNotNull(auditBO.getAuditBelongName()),RepairTicketDO::getAuditBelongName,auditBO.getAuditBelongName())
                .set(BeanUtils.isNotNull(auditBO.getAuditTime()),RepairTicketDO::getAuditTime,auditBO.getAuditTime())
                .eq(RepairTicketDO::getRepairId, auditBO.getBusinessId()));
    }
}
